package com.lechi.web.service.impl;

import com.lechi.web.common.enums.ErrorCodeEnum;
import com.lechi.web.common.exception.ServiceException;
import com.lechi.web.framework.log.IotExceptionLog;
import com.lechi.web.service.IIotAndroidDeviceService;
import com.lechi.web.system.domain.IotAndroidDeviceBo;
import com.lechi.web.system.mapper.iot.IotAndroidDeviceMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.CompletableFuture;

/**
 * 设备Service业务层处理
 *
 * @author yangguang
 */
@Slf4j
@Service("IIotAndroidDeviceService")
@RequiredArgsConstructor
public class IotAndroidDeviceServiceImpl implements IIotAndroidDeviceService {

    private final IotAndroidDeviceMapper iotAndroidDeviceMapper;

    /**
     * 查询设备
     *
     * @param iotDeviceBo 设备主键
     * @return 设备
     */
    @Override
    public IotAndroidDeviceBo selectIotAndroidDevice(IotAndroidDeviceBo iotDeviceBo) {
        return iotAndroidDeviceMapper.selectIotAndroidDevice(iotDeviceBo);
    }


    /**
     * 新增设备
     *
     * @param iotDeviceBo 设备
     */
    @Override
    public void insertIotAndroidDevice(IotAndroidDeviceBo iotDeviceBo) {
        try {
            iotAndroidDeviceMapper.insertIotAndroidDevice(iotDeviceBo);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "新增android设备", method, iotDeviceBo));
            throw new ServiceException(ErrorCodeEnum.ADD_ERROR.getValue(), ErrorCodeEnum.ADD_ERROR.getKey());
        }
    }

    /**
     * 修改设备
     *
     * @param iotDeviceBo 设备
     */
    @Override
    public void updateIotAndroidDevice(IotAndroidDeviceBo iotDeviceBo) {
        try {
            iotAndroidDeviceMapper.updateIotAndroidDevice(iotDeviceBo);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "修改android设备", method, iotDeviceBo));
            throw new ServiceException(ErrorCodeEnum.EDIT_ERROR.getValue(), ErrorCodeEnum.EDIT_ERROR.getKey());
        }
    }
}
